package com.xckj.cycling.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.xckj.cycling.R;
import com.xckj.cycling.databinding.ActivityRegisterBinding;
import com.xckj.cycling.framework.BaseActivity;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.utils.CheckFormatUtil;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 注册
 * @Date: 2023/3/1 15:20
 */
public class RegisterActivity extends BaseActivity<ActivityRegisterBinding> {

    @Override
    protected int loadLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void setTitle() {
        super.setTitle();
        dataBinding.viewTitle.tvTitle.setText(getString(R.string.register));
        dataBinding.viewTitle.tvRight.setText(getString(R.string.login));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        dataBinding.viewTitle.tvRight.setOnClickListener(this);
        dataBinding.cvRegisterCountdown.setOnClickListener(this);
        dataBinding.btnRegister.setOnClickListener(this);
        dataBinding.btnFacebookLogin.setOnClickListener(this);
        dataBinding.btnGoogleLogin.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        String mailbox = dataBinding.etMailbox.getText().toString();
        String code = dataBinding.etVerificationCode.getText().toString();
        String password = Objects.requireNonNull(dataBinding.etPassword.getText()).toString();
        String confirmPassword = Objects.requireNonNull(dataBinding.etConfirmPassword.getText()).toString();
        switch (view.getId()) {
            case R.id.tv_right:
                login();
                break;
            case R.id.cv_register_countdown:
                sendCode(mailbox);
                break;
            case R.id.btn_register:
                register(mailbox, code, password, confirmPassword);
                break;
            case R.id.btn_facebook_login:
                facebookLogin();
                break;
            case R.id.btn_google_login:
                googleLogin();
                break;
            default:
                break;
        }
    }

    private void login() {
        finish();
    }

    private void sendCode(String mailbox) {
        if (TextUtils.isEmpty(mailbox)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (!CheckFormatUtil.isMailbox(mailbox)) {
            showMessage(getString(R.string.mailbox_error));
            return;
        }
        showMessage(getString(R.string.send_code_hint));
        dataBinding.cvRegisterCountdown.start();
    }

    private void register(String mailbox, String code, String password, String confirmPassword) {
        if (TextUtils.isEmpty(mailbox)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (!CheckFormatUtil.isMailbox(mailbox)) {
            showMessage(getString(R.string.mailbox_error));
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showMessage(getString(R.string.code_null_hint));
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showMessage(getString(R.string.password_hint));
            return;
        }
        if (password.length() < Constant.PASSWORD_MIN_LENGTH) {
            showMessage(getString(R.string.password_length_error));
            return;
        }
        if (TextUtils.isEmpty(confirmPassword)) {
            showMessage(getString(R.string.password_null_hint));
            return;
        }
        if (confirmPassword.length() < Constant.PASSWORD_MIN_LENGTH) {
            showMessage(getString(R.string.password_length_error));
            return;
        }
        if (!password.equals(confirmPassword)) {
            showMessage(getString(R.string.password_compare));
            return;
        }
        goToRegister(mailbox, code, password, confirmPassword);
    }

    private void goToRegister(String mailbox, String code, String password, String confirmPassword) {
        startActivity(new Intent(this, PerfectRegisterActivity.class));
    }

    private void facebookLogin() {
    }

    private void googleLogin() {
    }
}
